{% extends "admin/change_list.html" %}
{% load i18n admin_list admin_static admin_urls cms_admin cms_js_tags cms_static cms_tags %}

{# TODO might not need that #}
{% block title %}{% trans "List of pages" %}{% endblock %}
{% block bodyclass %}{{ block.super }} change-list cms-pagetree-wrapper{% endblock %}
{% block coltype %}flex{% endblock %}
{% block date_hierarchy %}{% endblock %}
{% block pagination %}{% endblock %}

{% block extrahead %}
    {{ block.super }}
    {# INFO: we need to add styles here instead of "extrastyle" to avoid conflicts with adminstyle #}
    <link rel="stylesheet" href="{% static_with_version 'cms/css/cms.base.css' %}">
    <link rel="stylesheet" href="{% static_with_version 'cms/css/cms.pagetree.css' %}">
    <script src="{% static_with_version 'cms/js/dist/bundle.admin.base.min.js' %}"></script>
    <script src="{% static_with_version 'cms/js/dist/bundle.admin.pagetree.min.js' %}"></script>
{% endblock extrahead %}

{% if not is_popup %}
    {% block breadcrumbs %}
        <div class="breadcrumbs cms-pagetree-breadcrumbs">
            <a href="{% url 'admin:index' %}">{% trans "Home" %}</a> &rsaquo;
            <a href="{% url 'admin:app_list' app_label=opts.app_label %}">{{ opts.app_config.verbose_name }}</a> &rsaquo;
            {{ opts.verbose_name_plural|capfirst|escape }}
            {# TODO might remove this or add reset #}
            {% if request.GET.q or request.POST.q %}
            &rsaquo; {% trans "Search" %}
            {% endif %}
        </div>
    {% endblock %}
{% endif %}

{% block content_title %}{% endblock %}

{% block content %}
    {% spaceless %}
    <div id="content-main">
        <div class="cms-pagetree-root module{% if tree.is_filtered %} filtered{% endif %}" id="changelist">
            <div class="cms-pagetree cms-pagetree-header">
                <div class="cms-pagetree-header-cell cms-pagetree-header-cell-fluid">
                    <div class="cms-pagetree-header-row cms-pagetree-search-container">
                        <div class="cms-pagetree-header-cell">
                            <div class="cms-pagetree-header-title">
                                <h1>
                                    {% trans "Page Tree" %} <span>({{ tree.site.name }})</span>
                                </h1>
                            </div>

                            {% if tree.is_filtered or request.GET.q %}
                                {# INFO: show reset button when filtering is active #}
                                <a href="{% url opts|admin_urlname:'changelist' %}" class="cms-pagetree-header-cell cms-pagetree-header-search-reset">{% trans "Reset filter"|lower %}</a>
                            {% endif %}
                        </div>
                        <div class="cms-pagetree-header-cell cms-pagetree-header-cell-search">
                            {# INFO: visible search field with filtering #}
                            <form method="get" class="cms-pagetree-header-search js-cms-pagetree-header-search">
                                <label for="field-searchbar" class="cms-hidden">{% trans "Search" %}</label>
                                <div class="cms-pagetree-header-filter">
                                    <input type="text" size="40" name="q" id="field-searchbar" value="{{ tree.query }}" placeholder="{% trans "Search" %}">
                                    <div class="cms-pagetree-header-filter-trigger js-cms-pagetree-header-filter-trigger">
                                        <a href="#"><span class="cms-icon cms-icon-arrow"></span></a>
                                    </div>
                                    <div class="cms-pagetree-header-filter-container js-cms-pagetree-header-filter-container">
                                        {% for field in changelist_form.visible_fields %}
                                            {% render_filter_field request field %}
                                        {% endfor %}
                                        <a href="#" class="cms-pagetree-header-search-close js-cms-pagetree-header-search-close">
                                            <span class="cms-icon cms-icon-close"></span>
                                        </a>
                                    </div>
                                </div>
                                <button type="submit" class="cms-pagetree-header-search-btn">
                                    <span class="cms-icon cms-icon-search"></span>
                                </button>
                            </form>
                        </div>
                    </div>
                </div>

                <div class="cms-pagetree-header-cell cms-pagetree-header-body">
                    {# INFO: hidden search field with dynamic content #}
                    <div class="js-cms-pagetree-header-search-copy cms-hidden">
                        <div id="toolbar">
                            <form id="changelist-search" method="get">
                                <div><!-- DIV needed for valid HTML -->
                                    <label for="searchbar"><img src="{% static "admin/img/search.svg" %}" alt="Search" /></label>
                                    <input type="text" size="40" name="{{ search_var }}" value="{{ tree.query }}" id="searchbar" autofocus />
                                    <input type="submit" value="{% trans 'Search' %}" />
                                    {% for pair in changelist_form.get_filter_items %}
                                        {% if pair.0 != search_var %}<input type="hidden" name="{{ pair.0 }}" value="{{ pair.1 }}"/>{% endif %}
                                    {% endfor %}
                                </div>
                            </form>
                        </div>
                    </div>
                    <div class="cms-clear-mobile"></div>

                    {# INFO: dropdown for changing sites and recover deleted pages #}
                    <div class="cms-pagetree-sites-list cms-pagetree-dropdown js-cms-pagetree-dropdown">
                        <a href="#" class="cms-pagetree-dropdown-trigger js-cms-pagetree-dropdown-trigger">
                            <span class="cms-icon cms-icon-squares"></span>
                        </a>
                        <div class="cms-pagetree-dropdown-menu cms-pagetree-dropdown-menu-condensed cms-pagetree-dropdown-menu-arrow-top-right js-cms-pagetree-dropdown-menu">
                            <ul class="cms-pagetree-dropdown-menu-inner">
                                <li>
                                    <span class="label">{% trans "Sites" %}</span>
                                </li>
                                {% for site in tree.sites %}
                                    <li{% if site.pk == tree.site.pk %} class="active"{% endif %}>
                                        <a href="#{{ site.pk }}" class="js-cms-pagetree-site-trigger" data-id="{{ site.pk }}">{{ site.name }}</a>
                                    </li>
                                {% endfor %}
                                {% if has_recover_permission %}
                                    <li class="cms-pagetree-dropdown-separator">&nbsp;</li>
                                    <li>
                                        <a href="{% url opts|admin_urlname:'recoverlist' %}" class="recoverlink">
                                            {% blocktrans with opts.verbose_name_plural|escape as name %}
                                                Restore deleted {{ name }}
                                            {% endblocktrans %}
                                        </a>
                                    </li>
                                {% endif %}
                            </ul>
                        </div>
                        {# INFO: hidden site form when using the dropdown site switcher #}
                        <form method="post" class="js-cms-pagetree-site-form cms-hidden">
                            <select name="site">
                                {% for site in tree.sites %}
                                    <option value="{{ site.pk }}">{{ site.name }}</option>
                                {% endfor %}
                            </select>
                            {% csrf_token %}
                        </form>
                    </div>

                    {# INFO: "new page" button #}
                    {% if has_add_permission %}
                        <a href="{% url opts|admin_urlname:'add' %}" class="cms-pagetree-header-create cms-btn cms-btn-toolbar cms-btn-action">
                            {% blocktrans with opts.verbose_name|title as name %}
                                New {{ name }}
                            {% endblocktrans %}
                        </a>
                    {% endif %}
                </div>
            </div>

            <div class="clear"></div>

            <form id="changelist-form" action="" method="post" novalidate>{% csrf_token %}
                <div class="cms-pagetree cms-pagetree-section">
                    <h2>{% trans "Main Navigation" %}</h2>
                    <div class="cms-pagetree-section-nav">
                        {% if has_add_permission %}
                            <div class="cms-pagetree-dropdown js-cms-pagetree-dropdown">
                                <a href="#root" data-node-id="#root" data-id="#root" class="js-cms-pagetree-dropdown-trigger js-cms-pagetree-options cms-pagetree-dropdown-trigger cms-btn cms-btn-default cms-btn-no-border cms-icon cms-icon-menu">
                                    <span class="sr-only">{% trans "Options" %}</span>
                                </a>

                                <div class="js-cms-pagetree-dropdown-menu cms-pagetree-dropdown-menu cms-pagetree-dropdown-menu-arrow-right-top">
                                    <ul class="cms-pagetree-dropdown-menu-inner">
                                        <li>
                                            <a href="#" data-node-id="#root" data-id="#root" class="js-cms-tree-item-paste cms-pagetree-dropdown-item-disabled">
                                                <span class="cms-icon cms-icon-alias"></span>
                                                <span>{% trans "Paste" %}</span>
                                            </a>
                                        </li>
                                    </ul>
                                </div>
                            </div>
                        {% endif %}
                    </div>
                </div>

                {# INFO: javascript is loaded from cms.pagetree.js #}
                {% if tree.has_items %}
                    <div class="cms-pagetree cms-pagetree-container">
                        <div class="cms-pagetree-jstree js-cms-pagetree" data-json='{
                            "urls": {
                                "tree": "{% url opts|admin_urlname:'get_tree' %}",
                                "move": "./{id}/move-page/",
                                "copy": "./{id}/copy-page/",
                                "copyPermission": "./{id}/dialog/copy/",
                                "theme": "{% static 'cms/css/' %}"
                            },
                            "csrf": "{{ csrf_token }}",
                            "static": "{{ STATIC_URL }}",
                            "permission": {{ CMS_PERMISSION|bool }},
                            "debug": {{ DEBUG|bool }},
                            "filtered": {% if tree.is_filtered %}true{% else %}false{% endif %},
                            "site": {{ tree.site.pk }},
                            "hasAddRootPermission": {{ has_add_permission|yesno:"true,false" }},
                            "lang": {
                                "code": "{{ preview_language|lower }}",
                                "success": "{% filter escapejs %}{% trans "Successfully moved" %}{% endfilter %}",
                                "changes": "{% filter escapejs %}{% trans "Changes within the tree might require a refresh." %}{% endfilter %}",
                                "error": "{% filter escapejs %}{% trans "Error:" %}{% endfilter %}",
                                "apphook": "{% filter escapejs %}{% trans "This page cannot be copied because an application is attached to it. See the Page's Advanced settings to manage apphooks." %}{% endfilter %}",
                                "publish": "{% filter escapejs %}{% trans "Are you sure you want to § this page?" %}{% endfilter %}",
                                "reload": "{% trans "Reload" %}",
                                "loading": "{% trans "Loading..." %}",
                                "newNode": "{% trans 'New node' %}",
                                "nodes": "{% trans 'nodes' %}"
                            },
                            "columns": [{
                                "title": "&nbsp;",
                                "key": "",
                                "width": "100%"
                            }, {
                                "title": "&nbsp;",
                                "key": "view",
                                "cls": "jstree-grid-cell-regular-options"
                            }, {
                                "title": "{{ _('View')|escapejs }}",
                                "key": "preview"
                            }, {% for lang in site_languages %}{
                                "title": "<a href=\"{% url opts|admin_urlname:'changelist' %}?language={{ lang }}\" class=\"js-pagetree-header-lang cms-pagetree-header-lang{% if preview_language == lang %} active{% endif %}\">{{ lang|upper }}</a>",
                                "key": "{{ lang }}"
                            }, {% endfor %} {
                                "title": "{{ _('Menu')|escapejs }}",
                                "key": "menu"
                            },
                            {% block extra_cols %}
                            {% endblock %}
                            {
                                "title": "&nbsp;",
                                "key": "options"
                            }]
                        }' data-settings-url="{% cms_admin_url "cms_usersettings_session_store" %}">
                            <ul>
                                {% if tree.is_filtered %}
                                    {# INFO: load html data source when filtering #}
                                    {% show_admin_menu_for_pages tree.items depth=0 %}
                                {% endif %}
                            </ul>
                        </div>
                    </div>
                    {% include "admin/cms/page/tree/legend.html" %}
                {% else %}
                    {# INFO: show add a page button when there are no items #}
                    <div class="cms-pagetree cms-pagetree-empty js-cms-pagetree" data-json='{
                        "empty": true,
                        "urls": {
                            "tree": "{% url opts|admin_urlname:'get_tree' %}",
                            "move": "./{id}/move-page/",
                            "copy": "./{id}/copy-page/",
                            "copyPermission": "./{id}/dialog/copy/",
                            "theme": "{% static 'cms/css/' %}"
                        },
                        "csrf": "{{ csrf_token }}",
                        "static": "{{ STATIC_URL }}",
                        "permission": {{ CMS_PERMISSION|bool }},
                        "debug": {{ DEBUG|bool }},
                        "filtered": {% if tree.is_filtered %}true{% else %}false{% endif %},
                        "site": {{ tree.site.pk }},
                        "hasAddRootPermission": {{ has_add_permission|yesno:"true,false" }},
                        "lang": {
                            "code": "{{ preview_language|lower }}",
                            "success": "{% filter escapejs %}{% trans "Successfully moved" %}{% endfilter %}",
                            "changes": "{% filter escapejs %}{% trans "Changes within the tree might require a refresh." %}{% endfilter %}",
                            "error": "{% filter escapejs %}{% trans "Error:" %}{% endfilter %}",
                            "apphook": "{% filter escapejs %}{% trans "This page cannot be copied because an application is attached to it. See the Page's Advanced settings to manage apphooks." %}{% endfilter %}",
                            "publish": "{% filter escapejs %}{% trans "Are you sure you want to § this page?" %}{% endfilter %}",
                            "reload": "{% trans "Reload" %}",
                            "loading": "{% trans "Loading..." %}",
                            "newNode": "{% trans 'New node' %}",
                            "nodes": "{% trans 'nodes' %}"
                        },
                        "columns": [{
                            "title": "&nbsp;",
                            "key": "",
                            "width": "100%"
                        }, {
                            "title": "&nbsp;",
                            "key": "view",
                            "cls": "jstree-grid-cell-regular-options"
                        }, {
                            "title": "{{ _('View')|escapejs }}",
                            "key": "preview"
                        }, {% for lang in site_languages %}{
                            "title": "<a href=\"{% url opts|admin_urlname:'changelist' %}?language={{ lang }}\" class=\"js-pagetree-header-lang cms-pagetree-header-lang{% if preview_language == lang %} active{% endif %}\">{{ lang|upper }}</a>",
                            "key": "{{ lang }}"
                        }, {% endfor %} {
                            "title": "{{ _('Menu')|escapejs }}",
                            "key": "menu"
                        },
                        {
                            "title": "&nbsp;",
                            "key": "options"
                        }]
                    }' data-settings-url="{% cms_admin_url "cms_usersettings_session_store" %}">
                        {% url opts|admin_urlname:'add' as add_url %}
                        {% blocktrans with object=opts.verbose_name|lower %}
                            <em>There is no {{ object }} around yet.</em>
                            <br>
                            <a href="{{ add_url }}" class="addlink">Add {{ object }}</a> now.
                        {% endblocktrans %}
                    </div>
                {% endif %}
            </form>

            {# INFO: used when copying nodes #}
            <div class="cms-tree-dialog js-cms-tree-dialog"></div>
        </div>
    </div>
    {% endspaceless %}
{% endblock content %}
